package web;

import java.net.URLDecoder;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Operator;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import bean.Institution;
import service.InstitutionS;

public class InstitutionW implements Controller {

	private String viewName;
	private InstitutionS institutionS;
	
	public InstitutionS getInstitutionS() {return institutionS;}
	public void setInstitutionS(InstitutionS institutionS) {this.institutionS = institutionS;}
	public String getViewName() {return viewName;}
	public void setViewName(String viewName) {this.viewName = viewName;}

	public ModelAndView handleRequest(HttpServletRequest request,
			                         HttpServletResponse response) throws Exception
    {
	
		ModelAndView mav=new ModelAndView();
		List model=new ArrayList();
		String dom=request.getParameter("dom");
		 
		Operator operator=(Operator)request.getSession().getAttribute("operator");
		if(null==operator)
		{ 
			mav.setViewName("redirect:/index.htm");
		    return mav;
		}
		if("2111".equals(dom))
		{
			String firston=request.getParameter("firston");
			String firstoff=request.getParameter("firstoff");
			String secondon=request.getParameter("secondon");
			String secondoff=request.getParameter("secondoff");
			int firstlater=0;
			int firstbefore=0;
			int secondlater=0;
			int secondbefore=0;
			if(!"".equals(request.getParameter("firstlater")))firstlater=Integer.parseInt(request.getParameter("firstlater"));
			if(!"".equals(request.getParameter("firstbefore")))firstbefore=Integer.parseInt(request.getParameter("firstbefore"));
			if(!"".equals(request.getParameter("secondlater")))secondlater=Integer.parseInt(request.getParameter("secondlater"));
			if(!"".equals(request.getParameter("secondbefore")))secondbefore=Integer.parseInt(request.getParameter("secondbefore"));
			String holiday=URLDecoder.decode(request.getParameter("holiday"),"utf-8");
			int forfeit=0;
			int allowminute=0;
			int moneyminute=0;
			int allowtimes=0;
			int attbonus=0;
			if(!"".equals(request.getParameter("forfeit")))    forfeit=Integer.parseInt(request.getParameter("forfeit"));
			if(!"".equals(request.getParameter("allowminute")))allowminute=Integer.parseInt(request.getParameter("allowminute"));
			if(!"".equals(request.getParameter("moneyminute")))moneyminute=Integer.parseInt(request.getParameter("moneyminute"));
			if(!"".equals(request.getParameter("allowtimes"))) allowtimes=Integer.parseInt(request.getParameter("allowtimes"));
			if(!"".equals(request.getParameter("attbonus")))   attbonus=Integer.parseInt(request.getParameter("attbonus"));
			Institution ins=new Institution();
			
			ins.setFirston(firston);
			ins.setFirstoff(firstoff);
			ins.setSecondon(secondon);
			ins.setSecondoff(secondoff);
			ins.setFirstlater(firstlater);
			ins.setFirstbefore(firstbefore);
			ins.setSecondlater(secondlater);
			ins.setSecondbefore(secondbefore);
			ins.setHoliday(holiday);
			ins.setForfeit(forfeit);
			ins.setAllowminute(allowminute);
			ins.setMoneyminute(moneyminute);
			ins.setAllowtimes(allowtimes);
			ins.setAttbonus(attbonus);
		
			Calendar calendar=Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_YEAR,1);
			
			ins.setStart(new Date(calendar.getTimeInMillis()));
			
			institutionS.saveInstitution(ins);
			
		}
		if("5101".equals(dom))
		{
			mav.setViewName(viewName+"edit");
			
			List institution=institutionS.getInstitution();
			
			model.add(institution);
			
			mav.addObject("model",model);
		}
		return mav;
	}
}
